/*
 * HSM Proxy Project.
 * Copyright (C) 2013 Frank Cornelis.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.admin.webapp;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import be.fedict.hsm.admin.webapp.security.RolesAllowed;
import be.fedict.hsm.entity.AdministratorEntity;
import be.fedict.hsm.model.admin.PrivilegesManagerBean;
import be.fedict.hsm.model.security.AdministratorRoles;

@Named("hsmProxyPrivileges")
@ConversationScoped
@RolesAllowed(AdministratorRoles.ADMINISTRATOR)
public class PrivilegesController implements Serializable {

	private static final Log LOG = LogFactory
			.getLog(PrivilegesController.class);

	private static final long serialVersionUID = 1L;

	@EJB
	private PrivilegesManagerBean privilegesManagerBean;

	@Inject
	private Conversation conversation;

	private AdministratorEntity selectedAdministrator;

	public PrivilegesController() {
		super();
		LOG.debug("constructor");
	}

	public List<AdministratorEntity> getList() {
		return this.privilegesManagerBean.getList();
	}

	public void selectAdministrator(ActionEvent actionEvent) {
		UIComponent component = actionEvent.getComponent();
		Map<String, Object> attributes = component.getAttributes();
		this.selectedAdministrator = (AdministratorEntity) attributes
				.get("selectedAdministrator");
		LOG.debug("selected admin: "
				+ this.selectedAdministrator.getCardNumber());
		if (this.conversation.isTransient()) {
			this.conversation.begin();
		}
	}

	public void approveAdministrator(ActionEvent actionEvent) {
		UIComponent component = actionEvent.getComponent();
		Map<String, Object> attributes = component.getAttributes();
		AdministratorEntity administrator = (AdministratorEntity) attributes
				.get("selectedAdministrator");
		this.privilegesManagerBean.approveAdministrator(administrator);
	}

	public void removeSelectedAdministrator(ActionEvent actionEvent) {
		LOG.debug("remove admin: " + this.selectedAdministrator.getCardNumber());
		this.privilegesManagerBean
				.removeAdministrator(this.selectedAdministrator);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Removed administrator "
						+ this.selectedAdministrator.getCardNumber(), null));
		this.selectedAdministrator = null;
		this.conversation.end();
	}
}
